Ext.require(['Ext.data.*', 'Ext.grid.*']);
if (Ext.grid.RowEditor) {
    Ext.apply(Ext.grid.RowEditor.prototype, {
        saveBtnText: '保存',
        cancelBtnText: '取消',
        errorsText: '错误信息',
        dirtyText: '已修改,你需要提交或取消变更'
    });
}
Ext.define('Org', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'id',
        useNull: true
    }, 'dwCode', 'dwName','dwType','dwOwner', 'Remark', 'ParentID', 'isgroup', 'orderID'],
    validations: [{
        type: 'length',
        field: 'dwName',
        min: 1
    }, {
        type: 'length',
        field: 'dwType'
    }, {
        type: 'length',
        field: 'orderID',
        min: 1
    }]
});
//所属部门
Ext.define('bmlist', {
    extend: 'Ext.data.Model',
    fields: ['id','dwCode','dwName']
});

Ext.onReady(function(){
var dwtype;
var treestore = Ext.create('Ext.data.TreeStore', {
    root: {
        id:'root',
        text:'全部',
        expanded: true
    },
    proxy: {
        type: 'ajax',
        url: 'yhpcOrgTreeAjax.aspx'
    }
});
var bmStore = Ext.create('Ext.data.Store', {
    model: 'bmlist',
    autoLoad: true,
    //autoSync: true,
    queryMode: 'local',
    proxy: {
        type: 'ajax',
        url: 'yhpcOrgAjax.aspx?action=read&type=公司部门',
        reader: {
            type: 'json',
            root: 'data'
        }
    }

});  
var treePanel = Ext.create('Ext.tree.Panel', {
    id: 'tree-panel',
    title: '单位分类',
    region: 'west',
    split: true,
    width: 250,
    minSize: 150,
    rootVisible: true,
    collapsible: true,
    floatable: true,
    autoScroll: true,
    store: treestore,
    dockedItems: [{
            xtype: 'toolbar',
            items: [{
                text: '刷新数据',
                iconCls: 'icon-grid',
                handler: function(){
                store.removeAll();
                treestore.load();
                }
            }]
    
    }]
});

treePanel.getSelectionModel().on('select', function(selModel, record) {
    if (!record.get('leaf')) {
        store.clearFilter(false);
        dwtype = record.getId()
        store.filter("ParentID",dwtype);
        store.load();
    }
});
    var store = Ext.create('Ext.data.Store', {
        //autoLoad: true,
        autoSync: true,
        model: 'Org',
        proxy: {
            type: 'ajax',
            api: {
            read: 'yhpcOrgAjax.aspx?action=read',
            create: 'yhpcOrgAjax.aspx?action=create',
            update: 'yhpcOrgAjax.aspx?action=update',
            destroy: 'yhpcOrgAjax.aspx?action=delete'
            },
            reader: {
                type: 'json' ,
                root: 'data'
            },
            writer: {
                type: 'json' ,
                root: 'data'      
            }},
        listeners: {
            write: function(store, operation){

                
            }
        }
    });
     
    var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');
    
    var grid = Ext.create('Ext.grid.Panel', {
        //renderTo: document.body,
        //layout:{type:"fit"},
        plugins: [rowEditing],
//        width: 400,
        //        height: 300,
        region: 'center',
        frame: true,
        title: '单位维护',
        store: store,
        //iconCls: 'icon-user',
        defaults: { xtype: 'textfield' }, 
        columns: [{
            text: '序号',

            sortable: true,
            width:60,
            dataIndex: 'orderID',
            emptyText: '填写序号',
            
            field: {
            xtype: 'numberfield'
            }
        }, {
            header: '分类',
            emptyText: '选择分类',
            width: 80,
            sortable: true,
            dataIndex: 'dwType',
            editor: {
                xtype: 'combobox',
                typeAhead: true,
                //triggerAction: 'all',
                selectOnTab: true,
                store: [
                    ['','无'],
                    ['二级单位', '二级单位'],
                    ['公司部门', '公司部门'],
                    ['公司领导', '公司领导']
                ]
                ,
                lazyRender: true,
                listClass: 'x-combo-list-small'
            }
        }, {
            text: '单位名称',
            sortable: true,
            flex: 1,
            width:100,
            dataIndex: 'dwName',
            field: {
                xtype: 'textfield'
            }
        }, {
            text: '是否有下级',
            sortable: true,
            width: 100,
            dataIndex: 'isgroup',
            editor: {
                xtype: 'combobox',
                typeAhead: true,
                //triggerAction: 'all',
                selectOnTab: true,
                store: [
                    ['0', '否'],
                    ['1', '是']
                ]
                ,
                lazyRender: true,
                listClass: 'x-combo-list-small'
            }
        }, {
            text: '所属单位',
            sortable: true,
            //flex: 1,
            width: 100,
            dataIndex: 'dwOwner',
            field: {
                xtype: 'textfield'
            }
//            editor: {
//                xtype: 'combobox',
//                typeAhead: true,
//                //triggerAction: 'all',生产部、调度所、发策部、科信部、安监部、基建部、营销部、公安部
//                selectOnTab: true,
//                displayField:'dwName',
//                valueField: 'dwName',
//                store:bmStore
////                store: [
////                    ['生产部', '生产部'],
////                    ['调度所', '调度所'],
////                    ['发策部', '发策部']
////                ]
//                ,
//                lazyRender: true,
//                listClass: 'x-combo-list-small'
//            }
        }],
        dockedItems: [{
            xtype: 'toolbar',
            items: [{
                text: '增加',
                iconCls: 'icon-add',
                handler: function(){
                // empty record
                    if(!dwtype)return;
                    store.insert(0, new Org({ParentID:dwtype,dwType:'',isgroup:'0',orderID:0}));
                    rowEditing.startEdit(0, 2);
                }
            }, '-', {
                itemId: 'delete',
                text: '删除',
                iconCls: 'icon-delete',
                disabled: true,
                handler: function(){
                    var selection = grid.getView().getSelectionModel().getSelection()[0];
                    if (selection) {
                        store.remove(selection);
                    }
                }
            }]
        }]
    });
    grid.getSelectionModel().on('selectionchange', function(selModel, selections){
        grid.down('#delete').setDisabled(selections.length === 0);
    });
    Ext.create('Ext.Viewport', {
        layout: {
            type: 'border',
            padding: 5
        },
        defaults: {
            split: true
        },
        items: [treePanel, grid]
        });
});
